package com.slkj.shilixiaoyuanapp.view.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.CommunityEntity;
import com.slkj.shilixiaoyuanapp.ui.main.community.CommunityAdapter;

/**
 * Created  on 2018-08-14
 *
 * @author 唐开阔
 * @describe 社区评论。
 */
public class CommunityCommentPWindow {
    private PopupWindow snsPopupWindow;
    private Context mContext;
    private View content;
    CommunityAdapter.CallBack callBack ;

    public CommunityCommentPWindow(Context mContext,int pos,CommunityEntity item) {
        this.mContext = mContext;
        content = LayoutInflater.from(mContext).inflate(R.layout.layout_pop_comment, null);
        snsPopupWindow = new PopupWindow(content, dip2px(mContext,140), dip2px(mContext,35));
        snsPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        snsPopupWindow.setOutsideTouchable(true);
        snsPopupWindow.setTouchable(true);
        snsPopupWindow.setAnimationStyle(R.style.COMMENT_ANIM_STYLE);
        TextView tvLike = content.findViewById(R.id.zan);
        tvLike.setText(item.isFabulous()?"取消":"赞");
        content.findViewById(R.id.like).setOnClickListener(v -> {
            if (callBack != null){
                callBack.fabulous(item.getId(),pos);
            }
            snsPopupWindow.dismiss();
        });
        content.findViewById(R.id.comment).setOnClickListener(v -> {
            if (callBack != null){
                callBack.comment(null,pos,1);
            }
            snsPopupWindow.dismiss();
        });
    }

    public void setCallBack(CommunityAdapter.CallBack callBack) {
        this.callBack = callBack;
    }

    public void show(View view){
        if (snsPopupWindow.isShowing()){
            snsPopupWindow.dismiss();
            return;
        }
        int heightMoreBtnView = view.getHeight();

        snsPopupWindow.showAsDropDown(view, -dip2px(mContext,140)-20,
                -(dip2px(mContext,35) + heightMoreBtnView) / 2);
    }
    public static int dip2px(Context var0, float var1) {
        float var2 = var0.getResources().getDisplayMetrics().density;
        return (int)(var1 * var2 + 0.5F);
    }
}
